<template>
  <div>
    <div class="box"></div>
    <div class="bigbut" v-show="!isFocus">
      <div class="but">
        <input
          type="text"
          placeholder="写跟帖..."
          class="inp"
          @focus="toopen"
        />

        <span class="right">
          <!-- 信息标 -->
          <van-icon
            name="chat-o"
            :badge="obj.comment_length ? obj.comment_length : null"
            @click="chat" />
          <!-- 收藏 -->
          <van-icon
            name="star"
            @click="star"
            :class="{ gostar: obj.has_star }" />
          <!-- 分享 -->
          <van-icon name="share " @click="share"
        /></span>
      </div>
    </div>
    <div v-show="isFocus" class="texts">
      <textarea
        ref="commtext"
        rows="5"
        placeholder="请输入跟帖内容..."
        v-model="content"
        @blur="nofocus"
      ></textarea>
      <div>
        <span class="send" @click="send()">发 送</span>
        <span class="send" @click="cancel()">取 消</span>
      </div>
    </div>
    <!-- <van-cell title="显示分享面板" @click="showShare = false" /> -->
    <van-share-sheet
      v-model="showShare"
      title="分享给好友"
      :options="options"
      @select="onSelect"
    />
  </div>
</template>

<script>
import { Toast } from "vant";
import { getuserstar, topostcomments } from "@/apis/postlists.js";
// import { userfollows, getuserstarlist } from "@/apis/user.js";
export default {
  props: {
    obj: {
      type: Object,
      required: true,
    },
    idobj: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {
      isFocus: false,
      //分享框
      showShare: false,
      // 分享数组
      options: [
        { name: "微信", icon: "wechat" },
        { name: "微博", icon: "weibo" },
        { name: "复制链接", icon: "link" },
        { name: "分享海报", icon: "poster" },
        { name: "二维码", icon: "qrcode" },
      ],
      content: "",
      onoff: true,
    };
  },
  methods: {
    //取消按钮事件
    cancel() {
      this.isFocus = false;
      //清空内容
      this.content = "";
      //给父组件触底了事件,告知我这边取消了
      //子组件中props中获取的数据不能进行修改,所以只能传递事件回去父组件进行间接修改数据
      this.$emit("mycancel");
    },
    //输入框失焦
    nofocus() {
      setTimeout(() => {
        //收起输入框
        this.isFocus = false;
        //清空输入框的内容
        this.content = "";
      }, 10);
    },
    //打开输入框
    toopen() {
      this.isFocus = true;
      setTimeout(() => {
        this.$refs.commtext.focus();
      }, 10);
    },
    //发布
    async send() {
      //判断输入的内容除去首尾的空格后字符串的长度不能为0
      //trim()去掉首尾的空格
      if (this.conent.trim().length == 0) {
        this.$toast.fail("内容不能空");
        return;
      }
      // console.log("点击了发送...");
      let data = { content: this.content };
      // idobj 有值的时候进行添加
      if (this.idobj) {
        // 添加回复的评论id
        data.parent_id = this.idobj.id;
      }
      let id = this.$route.params.id;
      let res = await topostcomments(id, data);
      this.$toast.success(res.data.message);
      //清空内容
      this.content = "";
      //隐藏输入框
      this.isFocus = false;
      this.$emit("totop");
    },
    //收藏
    async star() {
      // console.log(this.obj);
      let res = await getuserstar(this.obj.id);
      this.obj.has_star = !this.obj.has_star;
      // console.log(res);
      Toast.success(res.data.message);
      // console.log("点击了收藏...");
    },
    //信息图标去评论页
    //还没进行本页面点击的失效功能
    async chat() {
      let id = this.$route.params.id;
      this.$router.push({ path: "/mythread/" + id });
      // }
    },
    //分享
    async share() {
      // console.log("要去分享...");
      this.showShare = !this.showShare;
    },
    //分享模块内容事件
    async onSelect(option) {
      Toast.success("分享成功");
      this.showShare = false;
    },
  },
  components: {
    getuserstar,
  },
  watch: {
    //侦听回复按钮的数据变化
    idobj() {
      //只要点击了回复就会给idobj进行赋值,赋值就会触发侦听器
      //idobj 不为空时进行,只有触底了数据才能展开输入框,真听到为null 是不展开
      // console.log("经过的侦听器");
      if (this.idobj) {
        this.isFocus = true;
        //进行聚焦
        setTimeout(() => {
          this.$refs.commtext.focus();
        }, 10);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.texts {
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-around;
  background: #eee;
  padding: 10px;
  position: fixed;
  bottom: 0;
  width: 100%;
  z-index: 2;
  .send {
    width: 60px;
    height: 30px;
    border-radius: 10px;
    background: red;
    margin: 20px;
    display: block;
    line-height: 30px;
    color: #fff;
    text-align: center;
  }
  textarea {
    border: 1px solid rgba(255, 0, 0, 0.534);
    border-radius: 10px;
  }
}
.box {
  height: 50px;
}
.bigbut {
  height: 50px;
  width: 100%;
  position: fixed;
  bottom: 0;
  // padding: 0 15px;
  background: #eee;
  left: 0;

  .but {
    height: 50px;
    display: flex;
    justify-content: space-around;
    border-top: 1px solid #ccc;
    align-items: center;
    color: #999;
    width: 100%;
    font-size: 25px;
    .inp {
      border-radius: 15px;
      background: #ccc;
      border: none;
      width: 200px;
      height: 30px;
      line-height: 30px;
      font-size: 16px;
      text-indent: 20px;
      color: #000;
    }
    .right {
      width: 100px;
      display: flex;
      justify-content: space-between;
      .gostar {
        color: orange;
      }
    }
  }
}
</style>